const artistcataServe = require("../server/artist_cata")
class artistcateContral {
  async getcatelist(req, res) {
    const result = await artistcataServe.getcatalist()
    console.log(result[0])
    if (result[0].length) {
      return res.send({
        status: 0,
        data: result[0],
      })
    } else {
      res.cc("获取文章分类数据失败")
    }
  }
  async addcates(req, res) {
    const res1 = await artistcataServe.iscatenameexit(req.body.name)
    if (res1[0].length) {
      return res.cc("分类已经存在了")
    }
    const res2 = await artistcataServe.iscatealiasexit(req.body.alias)
    if (res2[0].length) {
      return res.cc("别名已经存在了")
    }
    const res3 = await artistcataServe.updatacate(req.body.name, req.body.alias)
    if (res3[0].affectedRows) {
      res.cc("添加成功", 0)
    } else {
      res.cc("添加失败")
    }
  }
  async deletecate(req, res) {
    const res1 = await artistcataServe.deletecate(req.params.id)
    if (res1[0].affectedRows) {
      res.cc("删除成功", 0)
    } else {
      res.cc("删除失败")
    }
  }
  async getcatasbyid(req, res) {
    const res1 = await artistcataServe.getcatasbyid(req.params.id)
    console.log(res1)
    res.send({
      status: 0,
      data: res1[0],
    })
  }
  async updatacatebyid(req, res) {
    const res1 = await artistcataServe.iscatenameexit(req.body.name)
    if (res1[0].length) {
      return res.cc("分类已经存在了")
    }
    const res2 = await artistcataServe.iscatealiasexit(req.body.alias)
    if (res2[0].length) {
      return res.cc("别名已经存在了")
    }
    const res3 = await artistcataServe.updatacatebyid(
      req.body.Id,
      req.body.name,
      req.body.alias
    )
    console.log(res3)
    if (res3[0].affectedRows) {
      res.cc("更新成功", 0)
    } else {
      res.cc("更新失败")
    }
  }
}

module.exports = new artistcateContral()
